//
//  AudioManager.m
//  CardWarrior
//
//  Created by Tekka Xie on 12-4-19.
//  Copyright (c) 2012年 Tencent. All rights reserved.
//

#import "AudioManager.h"
#import "GameDataManager.h"

@implementation AudioManager

@synthesize player;
@synthesize mCardoutID;
@synthesize mCardbackID;
@synthesize mCardscoreID;
@synthesize mBackID;
@synthesize mConfirmID;
@synthesize mSelectID;
@synthesize mSlideID;
@synthesize mStartvsID;

static AudioManager* pInstance = nil;

+ (id)sharedInstance
{
    if (pInstance == nil)
        pInstance = [[AudioManager alloc] init];    
    return pInstance;
}

- (id)init
{
    if (self = [super init])
    {
        @autoreleasepool 
        {
            NSString* path = nil; 
            CFURLRef  pathURL;
            
            path   = [[NSBundle mainBundle] pathForResource:@"CardoutSFX" ofType:@"wav"];
            pathURL = (__bridge CFURLRef)[NSURL fileURLWithPath:path];
            AudioServicesCreateSystemSoundID(pathURL, &mCardoutID);
            
            path   = [[NSBundle mainBundle] pathForResource:@"CardbackSFX" ofType:@"wav"];
            pathURL = (__bridge CFURLRef)[NSURL fileURLWithPath:path];
            AudioServicesCreateSystemSoundID(pathURL, &mCardbackID);
            
            path   = [[NSBundle mainBundle] pathForResource:@"CardscoreSFX" ofType:@"wav"];
            pathURL = (__bridge CFURLRef)[NSURL fileURLWithPath:path];
            AudioServicesCreateSystemSoundID(pathURL, &mCardscoreID);
            
            path   = [[NSBundle mainBundle] pathForResource:@"BackSFX" ofType:@"wav"];
            pathURL = (__bridge CFURLRef)[NSURL fileURLWithPath:path];
            AudioServicesCreateSystemSoundID(pathURL, &mBackID);
            
            path   = [[NSBundle mainBundle] pathForResource:@"ConfirmSFX" ofType:@"wav"];
            pathURL = (__bridge CFURLRef)[NSURL fileURLWithPath:path];
            AudioServicesCreateSystemSoundID(pathURL, &mConfirmID);
            
            path   = [[NSBundle mainBundle] pathForResource:@"SelectSFX" ofType:@"wav"];
            pathURL = (__bridge CFURLRef)[NSURL fileURLWithPath:path];
            AudioServicesCreateSystemSoundID(pathURL, &mSelectID);
            
            path   = [[NSBundle mainBundle] pathForResource:@"SlideSFX" ofType:@"wav"];
            pathURL = (__bridge CFURLRef)[NSURL fileURLWithPath:path];
            AudioServicesCreateSystemSoundID(pathURL, &mSlideID);
            
            path   = [[NSBundle mainBundle] pathForResource:@"StartvsSFX" ofType:@"wav"];
            pathURL = (__bridge CFURLRef)[NSURL fileURLWithPath:path];
            AudioServicesCreateSystemSoundID(pathURL, &mStartvsID);
        }
    }
    return self;
}

- (void)PlayBGM:(NSString*)bgmFileName
{
    NSString *bgmPath = [[NSBundle mainBundle] pathForResource:bgmFileName ofType:@"wav"];
    NSURL *bgmURL = [[NSURL alloc] initFileURLWithPath:bgmPath];
    
    player = [[AVAudioPlayer alloc] initWithContentsOfURL: bgmURL error: nil];
    [player prepareToPlay];
    [player setDelegate: self];
    [player setNumberOfLoops:-1];
    [player setVolume:1.0];
    [player play];
    
    if ([[GameDataManager sharedInstance] bgm] == 0)
        [player pause];
}

- (void)PlaySFX:(NSString*)sfxFileName
{	
    @autoreleasepool {
    if ([[GameDataManager sharedInstance] sfx] == 0)
        return ;
    
	if (sfxFileName == @"CardoutSFX") {
		AudioServicesPlaySystemSound(mCardoutID);
	}
	else if(sfxFileName == @"CardbackSFX"){
		AudioServicesPlaySystemSound(mCardbackID);
	}
	else if(sfxFileName == @"CardscoreSFX"){
		AudioServicesPlaySystemSound(mCardscoreID);
	}
	else if(sfxFileName == @"BackSFX"){
		AudioServicesPlaySystemSound(mBackID);
	}
	else if(sfxFileName == @"ConfirmSFX"){
		AudioServicesPlaySystemSound(mConfirmID);
	}
	else if(sfxFileName == @"SelectSFX"){
		AudioServicesPlaySystemSound(mSelectID);
	}
	else if(sfxFileName == @"SlideSFX"){
		AudioServicesPlaySystemSound(mSlideID);
	}
	else if(sfxFileName == @"StartvsSFX"){
		AudioServicesPlaySystemSound(mStartvsID);
	}
}
}	

- (void)SetBGMVolumn:(float)volumn
{
    [player setVolume:volumn];
}

- (void)EnableBGM
{
    [[GameDataManager sharedInstance] setBgm:1];
    [player play];
}

- (void)DisableBGM
{
    [[GameDataManager sharedInstance] setBgm:0];
    [player pause];
}

- (void)SwithBGMState
{
    int bgm = ![[GameDataManager sharedInstance] bgm];
    [[GameDataManager sharedInstance] setBgm:bgm];
    
    if (bgm)
        [self EnableBGM];
    else
        [self DisableBGM];
}

- (void)EnableSFX
{
    [[GameDataManager sharedInstance] setSfx:1];
}

- (void)DisableSFX
{
    [[GameDataManager sharedInstance] setSfx:0];
}

- (void)SwithSFXState
{
    [[GameDataManager sharedInstance] switchSFX];
}
@end
